package client.network;

import java.util.ArrayList;

import packages.Packages;

public class MessageMailBox {
	private ArrayList<String> outgoingMessages;
	private ArrayList<String> incomingMessages;
	private boolean gameActive;
	private String winnerMessage = "";
	private boolean startGame = false;
	private String drawersWord = "";
	private boolean drawersWordSet = false;
	private String[] clientList;
	private boolean clientListUpdated = false;
	private String lengthOfWord = "";
	private boolean clearCanvas = false;

	private boolean serverDisconnect;

	public MessageMailBox() {
		outgoingMessages = new ArrayList<>();
		incomingMessages = new ArrayList<>();
		gameActive = true;
	}

	public synchronized void addOutgoingMessage(String message) {
		outgoingMessages.add(message);
		notifyAll();
	}

	public synchronized String getOutgoingMessage() {
		while (outgoingMessages.isEmpty()) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String temp = outgoingMessages.remove(0);
		notifyAll();
		return temp;
	}

	public synchronized void addIncommingMessage(String message) {
		incomingMessages.add(message);
		notifyAll();
	}

	public synchronized Object getGUIUpdate() {
		Object temp = "";
		while (incomingMessages.isEmpty() && gameActive && !startGame
				&& !clientListUpdated && !drawersWordSet && !clearCanvas
				&& !serverDisconnect) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (serverDisconnect) {
			temp = "DISCONNECT";
		} else if (!gameActive) {
			temp = winnerMessage;
			gameActive = true;
		} else if (!incomingMessages.isEmpty()) {
			temp = incomingMessages.remove(0);
		} else if (!lengthOfWord.equals("")) {
			temp = "=" + lengthOfWord;
			lengthOfWord = "";
		} else if (startGame) {
			startGame = false;
			gameActive = true;
			temp = Packages.PACKAGE_NEW_GAME;
		} else if (clientListUpdated) {
			temp = clientList;
			clientListUpdated = false;
		} else if (clearCanvas) {
			temp = "!";
			clearCanvas = false;
		} else if (drawersWordSet) {
			temp = "#" + drawersWord;
			drawersWordSet = false;
		}
		notifyAll();
		return temp;
	}

	public synchronized void endGame(String[] parts) {
		gameActive = false;
		winnerMessage = "Congratulations " + parts[0]
				+ " guessed the correct word: " + parts[1] + " \n Press for next game!";
		notifyAll();
	}

	public synchronized void startGame() {
		startGame = true;
		notifyAll();

	}

	public synchronized void drawersWord(String message) {
		drawersWord = message;
		drawersWordSet = true;
		notifyAll();

	}

	public synchronized void updateClientList(String message) {
		clientList = message.split(",");
		clientListUpdated = true;
		notifyAll();
	}

	public synchronized void lengthOfWord(String message) {
		message = message.trim();
		lengthOfWord = message;

		notifyAll();
	}

	public synchronized void clearCanvas() {
		clearCanvas = true;
		notifyAll();

	}

	public synchronized void serverDisconnects() {
		serverDisconnect = true;
		notifyAll();
	}
}
